package RobotPlayer
{
	import BasicEngine.Vector2D;
	/**
	 * 一个计算合力的类
	 */
	public class Resultant
	{
		public var Robot:MoveEntity;
		public function Resultant(_robot:MoveEntity)
		{
			Robot = _robot;
		}
		
		/**
		 * 返回一个操控实体到达目标位置的力
		 */
		public function Seek(_targetPos:Vector2D):Vector2D
		{
			var desiredVelocity:Vector2D = ((_targetPos.Subtract(Robot.Pos)).Normalize()).CrossProduct(Robot.MaxSpeed);
			var result:Vector2D = desiredVelocity.Subtract(Robot.Velocity);
			return result;		    
		}
		
		/**
		 * 返回一个指向目标的力
		 */
		public function SendDirect(_targetPos:Vector2D):Vector2D
		{
			var desiredVelocity:Vector2D = ((_targetPos.Subtract(Robot.Pos)).Normalize()).CrossProduct(Robot.MaxSpeed);
			return desiredVelocity;
		}
		
		public function SendAngle(_angle:Number):Vector2D
		{
			var d:Vector2D = new Vector2D(1, 1).Normalize().CrossProduct(Robot.MaxSpeed);
			d.Angle = _angle;
			return d;
		}
		
		/**
		 * 到达目标
		 */
		public function Arrive(_targetPos:Vector2D):Vector2D
		{
			var f:int = 1000;
			var tweaker:Number = 0.45;
			var toTarget:Vector2D = _targetPos.Subtract(Robot.Pos);
			var dist:Number = toTarget.Length;
			if (dist > 1)
			{
				var speed:Number =  dist / f * tweaker;
				speed = Math.min(speed, Robot.MaxSpeed);
				var desiredVelocity:Vector2D = toTarget.CrossProduct(speed / dist);// ).CrossProduct(1 / dist);
				var result:Vector2D = desiredVelocity.Subtract(Robot.Velocity);
				return result;
			}
			else
			{
			  return new Vector2D(0, 0);
			}
		}
	}
}